package cn.wolfcode.rbac.service.Impl;

import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.mapper.EmployeeMapper;
import cn.wolfcode.rbac.qo.EmployeeQo;
import cn.wolfcode.rbac.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author xiahuihui
 * #Description EmployeeServiceImpl
 * #Date: 2022/7/2 09:36
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public List<Employee> findEmployeeAll() {
        List<Employee> employees = employeeMapper.selectAll();
        return employees;
    }

    @Override
    public void deleteEmployee(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        employeeMapper.deleteRole(id);
    }

    @Override
    public List<Employee> findByCondition(EmployeeQo qo) {
        return employeeMapper.selectByCondition(qo);
    }

    @Override
    public PageInfo<Employee> findByPage(EmployeeQo qo) {
        //为PageHelper设置分页信息
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        
        //执行条件查询的sql
        List<Employee> employees = employeeMapper.selectByCondition(qo);

        return new PageInfo<Employee>(employees);
    }

    @Override
    public void save(Employee employee, Long[] roleIds) {
        //区分当前操作是添加还是修改
        if(employee.getId() == null){
            //添加
            //对密码加密
            String password = employee.getPassword();
            password = DigestUtils.md5DigestAsHex(password.getBytes());
            employee.setPassword(password);
            //保存员工信息
            employeeMapper.insert(employee);

        }else {
            //修改
            employeeMapper.updateByPrimaryKey(employee);
            //删除员工拥有的角色
            employeeMapper.deleteRole(employee.getId());
        }
        //判断是否分配角色
        if(roleIds != null){
            //遍历出每一个角色id
            for(Long roleId:roleIds){
                //关联保存员工角色信息
                employeeMapper.insertEmpRole(employee.getId(),roleId);
            }
        }

    }

    @Override
    public Employee fingById(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public void importXls(MultipartFile file) {
        //读取上传的文件输入流
        try {
            InputStream inputStream = file.getInputStream();
            //以Excel的方式读取文件解析 poi
            Workbook workbook = new HSSFWorkbook(inputStream);
            //解析数据

            //1.获取第一张sheet
            //getSheet(SheetName):根据sheet的名字获取sheet
            //getSheetAt(sheetIndex):根据sheet的下表获取sheet
            Sheet sheet = workbook.getSheetAt(0);

            //2.循环获取sheet中的每一行
            //获取最后一行的下标(数据行，空行不算)
            int lastRowNum = sheet.getLastRowNum();
            //从第二行开始遍历，忽略标题行
            for(int i = 1 ; i <= lastRowNum ; i++){
                Row row = sheet.getRow(i);
                //获取row每一个单元格的数据
                String name = row.getCell(0).getStringCellValue();
                String email = row.getCell(1).getStringCellValue();
                String age = row.getCell(2).getStringCellValue();

                //将获取到的数据存储到Employee对象中
                Employee employee = new Employee();
                employee.setName(name);
                employee.setEmail(email);
                employee.setAge(Integer.parseInt(age));//在jdk1.7版本中支持了自动拆装箱 valueOf也可转

                //将获取到的数添加到数据库当中
                employeeMapper.insert(employee);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public Workbook exportXls() {

        //生成一个空Excel对象
        Workbook workbook = new HSSFWorkbook();

        //创建sheet
        Sheet sheet = workbook.createSheet("员工信息");

        //创建标题行
        Row row = sheet.createRow(0);

        //创建行中的单元格
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");
        row.createCell(3).setCellValue("部门名称");

        //查询到员工信息
        List<Employee> employees = employeeMapper.selectAll();
        //遍历员工信息，获取到每一个员工对象
        int index = 1;
        for (Employee employee:employees){

            //每一个员工对象占用一行
            //创建每一行
            Row row1 = sheet.createRow(index++);

            //创建单元格，将员工对象各个数据存储到单元格中
            row1.createCell(0).setCellValue(employee.getName());
            row1.createCell(1).setCellValue(employee.getEmail());
            row1.createCell(2).setCellValue(employee.getAge());
            row1.createCell(3).setCellValue(employee.getDepartment()!=null ? employee.getDepartment().getName() : null);

        }
        return workbook;
    }

}
